var appModule = angular.module("efa", []);

appModule.factory('Data', function() {
	return {
		id : 'pro1',
		name : '余额宝',
		profit : [ 5.91, 5.92, 5.93, 5.94, 5.95, 5.96, 5.97 ],
		company : '支付宝'
	};
});

appModule.factory('ProList', function() {
	return [ {
		id : 'pro1',
		name : '余额宝',
		profit : [ 5.91, 5.92, 5.93, 5.94, 5.95, 5.96, 5.97 ],
		company : '支付宝'
	}, {
		id : 'pro2',
		name : '余额宝2',
		profit : [ 6.91, 6.92, 6.93, 6.94, 6.95, 6.96, 6.97 ],
		company : '支付宝'
	}, {
		id : 'pro3',
		name : '余额宝3',
		profit : [ 6.14, 6.13, 6.26, 6.13, 6.35, 6.16, 6.37 ],
		company : '支付宝'
	} ];
});

appModule.controller('ProListCtrl', function($rootScope, $scope, $filter, Data,
		ProList) {
	$scope.proList = ProList;

	$scope.selectPro = function(id) {
		Data = $filter('filter')(ProList, {
			"id" : id
		})[0];
		$rootScope.$broadcast('pro', Data);
	};

	var selected = $scope.selected = [];

	$scope.checkPro = function($event, id) {
		var checkbox = $event.target;
		var action = (checkbox.checked ? 'add' : 'remove');
		if (action == 'add' & selected.indexOf(id) == -1)
			selected.push(id);
		if (action == 'remove' && selected.indexOf(id) != -1)
			selected.splice(selected.indexOf(id), 1);

		$rootScope.$broadcast('selected', selected);
	};
});

appModule.controller('ProInfoCtrl', function($scope, Data) {
	$scope.proInfo = Data;
	$scope.inputVal = 10000;

	$scope.$on('pro', function(event, Data) {
		$scope.proInfo = Data;
	});

});

appModule.controller('NewsCtrl', function($scope, $filter, Data) {
	$scope.proInfo = Data;
	$scope.$on('pro', function(event, Data) {
		$scope.proInfo = Data;
	});

	//	
	// var p = $http({
	// method: 'GET',
	// url: '/json'
	// });
	// p.success(function(response, status, headers, config){
	// $scope.name = response.name;
	// });
	$scope.newslist = [ {
		title : "新浪：周小川表示不会取缔余额宝",
		url : "",
		time : "2014-2-12"
	}, {
		title : "搜狐：余额宝近期可能下调收益率",
		url : "",
		time : "2014-2-10"
	} ];
});

appModule.controller('ChartsCtrl',
		function($scope,$filter, ProList) {
			xitems = [ change_date(-6), change_date(-5), change_date(-4),
					change_date(-3), change_date(-2), change_date(-1),
					change_date(0) ];

			var datas = [];
			angular.forEach(ProList, function(item) {
				dataItem = {
					name : item.name,
					data : item.profit
				};
				datas.push(dataItem);
			});

			$scope.$on('selected', function(event, Data) {
				datas= [];
				angular.forEach(Data, function(item) {
					temp = $filter('filter')(ProList, {
						"id" : item
					})[0];
					dataItem = {
						name : temp.name,
						data : temp.profit
					};
					datas.push(dataItem);

				});
				loadCharts(xitems, datas);
			});

			loadCharts(xitems, datas);

		});
